import { parse } from 'url';
import webpath from '@/utils/webpath';
// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        id: `李海波 id ${i}`,
        nickname: `李海波 nickname ${i}`,
        name: `李海波 name ${i}`,
        value: `李海波 value ${i}`,
        createtime: `李海波 createtime ${i}`,
        type: `李海波 type ${i}`,
        updatable: `李海波 updatable ${i}`,
        creator: `李海波 creator ${i}`,
        status: `李海波 status ${i}`,
        orderid: `李海波 orderid ${i}`,
        memo: `李海波 memo ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}


let areaTree = [];
	areaTree.push(
	  { title: '学校银行', key: '5',org_id:'333',org_code:"00000",org_name:'中国移动' },
      { title: '姜旭聪你想什么呢', key:'00000000',org_id:'4444',org_code:"00000" },
      { title: '电脑电脑', key: '7', isLeaf: true,org_id:'66',org_code:"00000" },
      { title: '电视机', key: '8', isLeaf: true,org_id:'77',org_code:"00000" },
      { title: '电冰箱', key: '9', isLeaf: true,org_id:'88',org_code:"00000" },
      { title: '洗衣机', key: '10', isLeaf: true,org_id:'99',org_code:"00000" },
      { title: '电动车', key: '11', isLeaf: true,org_id:'1111',org_code:"00000" },
      { title: '电饭锅', key: '12', isLeaf: true,org_id:'3333',org_code:"00000" },
	);

let dictionarysTree = [];
	dictionarysTree.push(
	    {
		    key: 1,
		    nickname: 'John Brown sr.',
		    name: 60,
		    value: 'New York No. 1 Lake Park',
		    children: [
		      {
		        key: 11,
		        nickname: 'John Brown',
		        name: 42,
		        value: 'New York No. 2 Lake Park',
		      },
		      {
		        key: 12,
		        nickname: 'John Brown jr.',
		        name: 30,
		        value: 'New York No. 3 Lake Park',
		        children: [
		          {
		            key: 121,
		            nickname: 'Jimmy Brown',
		            name: 16,
		            value: 'New York No. 3 Lake Park',
		          },
		        ],
		      },
		      {
		        key: 13,
		        nickname: 'Jim Green sr.',
		        name: 72,
		        value: 'London No. 1 Lake Park',
		        children: [
		          {
		            key: 131,
		            nickname: 'Jim Green',
		            name: 42,
		            value: 'London No. 2 Lake Park',
		            children: [
		              {
		                key: 1311,
		                nickname: 'Jim Green jr.',
		                name: 25,
		                value: 'London No. 3 Lake Park',
		              },
		              {
		                key: 1312,
		                nickname: 'Jimmy Green sr.',
		                name: 18,
		                value: 'London No. 4 Lake Park',
		              },
		            ],
		          },
		        ],
		      },
		    ],
		  },
		  {
		    key: 2,
		    nickname: 'Joe Black',
		    name: 32,
		    value: 'Sidney No. 1 Lake Park',
		  },
	);

function getProDictionarys(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = dictionarysTree;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}

function getAreaTree(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }
    const params = parse(url, true).query;
    let dataSource = areaTree;
    const result = {
        list: dataSource,
    };
    return res.json(result);
}

function deleteByPrimaryKeyForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProDictionarys(req, res, u);
}


function selectByPrimaryKeyForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProDictionarys(req, res, u);
}



function insertForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        id: `李海波 id ${i}`,
        nickname: `李海波 nickname ${i}`,
        name: `李海波 name ${i}`,
        value: `李海波 value ${i}`,
        createtime: `李海波 createtime ${i}`,
        type: `李海波 type ${i}`,
        updatable: `李海波 updatable ${i}`,
        creator: `李海波 creator ${i}`,
        status: `李海波 status ${i}`,
        orderid: `李海波 orderid ${i}`,
        memo: `李海波 memo ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getProDictionarys(req, res, u);
}


function updateForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getProDictionarys(req, res, u);
}


function deleteByMapForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProDictionarys(req, res, u);
}



function updateByMapForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProDictionarys(req, res, u);
}

function getOneForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProDictionarys(req, res, u);
}

function getAllForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProDictionarys(req, res, u);
}

function queryPageForProDictionarys(req, res, u, b) {


    return getProDictionarys(req, res, u);
}

function countForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProDictionarys(req, res, u);
}


function insertBatchForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProDictionarys(req, res, u);
}



function deleteBatchForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProDictionarys(req, res, u);
}


function updateBatchForProDictionarys(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProDictionarys(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/prodictionarys/delete': deleteByPrimaryKeyForProDictionarys,
    'GET /${webpath}/web/api/service/prodictionarys/get': selectByPrimaryKeyForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/insert': insertForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/update': updateForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/deleteByMap': deleteByMapForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/updateByMap': updateByMapForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/getOne': getOneForProDictionarys,
    'POST /platform_server/web/api/service/prodictionarys/getAll': getAreaTree,
    'POST /platform_server/web/api/service/prodictionarys/queryPage': queryPageForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/count': countForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/insertBatch': insertBatchForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/deleteBatch': deleteBatchForProDictionarys,
    'POST /${webpath}/web/api/service/prodictionarys/updateBatch': updateBatchForProDictionarys,
    
    'POST /platform_server/web/api/service/prodictionaryschildren/queryPage': queryPageForProDictionarys,
};

